package models

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/astaxie/beego/orm"
)

//商品基础信息
type Goods struct {
	Id                 int64
	Goods_name         string
	Goods_type_id      int64
	Goods_introduction string
	Goods_state        int
	Goods_stock        int64
	Get_integral       int64
	Pay_type           int
	Buy_limit          int64
	Sort               int
	Is_hot             int
	Created            time.Time
	Update             time.Time
}

//商品规格信息
type GoodsFormat struct {
	Id            int64   `json:"id"`
	Filter_id     string  `json:"filter_id"`
	Goods_numbers string  `json:"goods_numbers"`
	Goods_price   float32 `json:"goods_price"`
	Goods_stock   int64   `json:"goods_stock"`
	Goods_weight  float32 `json:"goods_weight"`
}

//商品SEO信息
type GoodsSeo struct {
	Id           int64
	Goods_id     int64
	Another_name string
	Keyword      string
	Description  string
	Created      time.Time
	Update       time.Time
}

func (g *Goods) TableName() string {
	return "shop_goods"
}

func init() {
	orm.RegisterModel(new(Goods), new(GoodsFormat), new(GoodsSeo))
}

//新增商品
func AddGoodsData(goods_type_id int64, goods_name string, goods_introduction string, goods_state int, goods_stock int64, get_integral int64, pay_type int, buy_limit int64, sort int, is_hot int, goods_format string) (int64, string) {
	o := orm.NewOrm()

	//添加商品基础信息
	var t = time.Now()
	var goods = Goods{}
	goods.Goods_type_id = goods_type_id
	goods.Goods_name = goods_name
	goods.Goods_introduction = goods_introduction
	goods.Goods_state = goods_state
	goods.Goods_stock = goods_stock
	goods.Get_integral = get_integral
	goods.Pay_type = pay_type
	goods.Buy_limit = buy_limit
	goods.Sort = sort
	goods.Is_hot = is_hot
	goods.Created = t
	goods.Update = t

	goods_id, err := o.Insert(&goods)

	if err != nil {
		return CODE1006, MSG1006
	} else {
		//字符串转结构体
		var goodsFormat []GoodsFormat
		json.Unmarshal([]byte(goods_format), &goodsFormat)
		formatArr := goodsFormat
		for i := 0; i < len(formatArr); i++ {
			_, formatErr := o.Raw("INSERT INTO shop_goods_format (filter_id,goods_id,goods_numbers,goods_price,goods_stock,goods_weight,`created`,`update`) VALUES (?,?,?,?,?,?,?,?) ", formatArr[i].Filter_id, goods_id, formatArr[i].Goods_numbers, formatArr[i].Goods_price, formatArr[i].Goods_stock, formatArr[i].Goods_weight, t, t).Exec()
			if formatErr != nil {
				return CODE1006, MSG1006
			}
		}
		return CODE1000, MSG1000
	}
}

//修改商品
func UpdateGoodsData(goods_id int64, goods_type_id int64, goods_name string, goods_introduction string, goods_state int, goods_stock int64, get_integral int64, pay_type int, buy_limit int64, sort int, is_hot int, goods_format string) (int64, string) {
	o := orm.NewOrm()
	var t = time.Now()
	var goods = Goods{}
	goods.Id = goods_id

	if o.Read(&goods) == nil {
		//修改商品信息
		goods.Goods_type_id = goods_type_id
		goods.Goods_name = goods_name
		goods.Goods_introduction = goods_introduction
		goods.Goods_state = goods_state
		goods.Goods_stock = goods_stock
		goods.Get_integral = get_integral
		goods.Pay_type = pay_type
		goods.Buy_limit = buy_limit
		goods.Sort = sort
		goods.Is_hot = is_hot
		goods.Update = t

		_, err := o.QueryTable("shop_goods").Filter("id", goods.Id).Update(orm.Params{
			"goods_type_id":      goods.Goods_type_id,
			"goods_name":         goods.Goods_name,
			"goods_introduction": goods.Goods_introduction,
			"goods_state":        goods.Goods_state,
			"goods_stock":        goods.Goods_stock,
			"get_integral":       goods.Get_integral,
			"pay_type":           goods.Pay_type,
			"buy_limit":          goods.Buy_limit,
			"sort":               goods.Sort,
			"is_hot":             goods.Is_hot,
			"update":             goods.Update,
		})

		if err != nil {
			return CODE1006, MSG1006
		} else {

			//字符串转结构体
			var goodsFormat []GoodsFormat
			json.Unmarshal([]byte(goods_format), &goodsFormat)
			fmt.Println(goodsFormat)
			formatArr := goodsFormat

			for i := 0; i < len(formatArr); i++ {

				//根据传来的规格ID来判断，加入ID为0则判断为新增规则，非0则判断为修改
				if formatArr[i].Id == 0 {
					_, formatInsertErr := o.Raw("INSERT INTO shop_goods_format (filter_id,goods_id,goods_numbers,goods_price,goods_stock,goods_weight,`created`,`update`) VALUES (?,?,?,?,?,?,?,?) ", formatArr[i].Filter_id, goods.Id, formatArr[i].Goods_numbers, formatArr[i].Goods_price, formatArr[i].Goods_stock, formatArr[i].Goods_weight, t, t).Exec()
					if formatInsertErr != nil {
						return CODE1006, MSG1006
					}
				} else {
					_, formatUpdateErr := o.Raw("UPDATE shop_goods_format SET filter_id=?,goods_id=?,goods_numbers=?,goods_price=?,goods_stock=?,goods_weight=?,`update`=? WHERE id=? ", formatArr[i].Filter_id, goods_id, formatArr[i].Goods_numbers, formatArr[i].Goods_price, formatArr[i].Goods_stock, formatArr[i].Goods_weight, t, formatArr[i].Id).Exec()
					if formatUpdateErr != nil {
						return CODE1006, MSG1006
					}
				}
			}
			return CODE1000, MSG1000
		}
	} else {
		return CODE1008, MSG1008
	}
}

//新增商品SEO
func AddGoodsSeoData(goods_id int64, another_name string, keyword string, description string) (int64, string) {
	o := orm.NewOrm()
	t := time.Now()
	var goodsSeo = GoodsSeo{Goods_id: goods_id, Another_name: another_name, Keyword: keyword, Description: description}
	_, err := o.Raw("INSERT INTO shop_goods_seo (goods_id,another_name,keyword,description,created,`update`) VALUES (?,?,?,?,?,?) ", goodsSeo.Goods_id, goodsSeo.Another_name, goodsSeo.Keyword, goodsSeo.Description, t, t).Exec()
	if err != nil {
		return CODE1006, MSG1006
	} else {
		return CODE1000, MSG1000
	}
}

//修改商品SEO
func UpdateGoodsSeo(id int64, goods_id int64, another_name string, keyword string, description string) (int64, string) {
	o := orm.NewOrm()
	t := time.Now()
	var goodsSeo = GoodsSeo{Id: id, Goods_id: goods_id, Another_name: another_name, Keyword: keyword, Description: description}

	_, err := o.Raw("UPDATE shop_goods_seo SET goods_id=?,another_name=?,keyword=?,description=?,`update`=? WHERE id=? ", goodsSeo.Goods_id, goodsSeo.Another_name, goodsSeo.Keyword, goodsSeo.Description, t, goodsSeo.Id).Exec()
	if err != nil {
		return CODE1006, MSG1006
	} else {
		return CODE1000, MSG1000
	}
}

//根据商品ID检测该商品是否存在
func CheckGoodsById(goods_id int64) (int64, string) {
	o := orm.NewOrm()
	var goods = Goods{Id: goods_id}
	err := o.Read(&goods)
	if err != nil || err == orm.ErrNoRows {
		return CODE1008, MSG1008
	} else {

		return CODE1000, MSG1000
	}
}

//根据商品ID检测该商品SEO是否存在
func CheckGoodsSeoById(goods_id int64) (int64, string) {
	o := orm.NewOrm()
	var goods = Goods{Id: goods_id}
	err := o.Raw("SELECT id FROM shop_goods_seo WHERE goods_id = ?", goods.Id).QueryRow(&goods)
	if err != nil || err == orm.ErrNoRows {
		return CODE1008, MSG1008
	} else {

		return CODE1000, MSG1000
	}
}
